﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities;
using DataAccess;

namespace Controller
{
    public class GetTableInformationController
    {
        public event GetTableInformationEventHandler GetTableInformationCompleted;
        public event GetTableInformationEventHandler GetFullDataOfTableCompleted;

        private void OnGetTableInformationCompleted(GetTableInformationEventArgs e)
        {
            if (GetTableInformationCompleted != null)
            {
                GetTableInformationCompleted(e);
            }
        }

        private void OnGetFullDataOfTableCompleted(GetTableInformationEventArgs e)
        {
            if (GetFullDataOfTableCompleted != null)
            {
                GetFullDataOfTableCompleted(e);
            }
        }

        public void GetTableInformation(ConnectionInformation CI, string TableName)
        {
            TableInformation result = new TableInformation();

            try
            {
                if (CI.DataBaseType == DataBaseType.MSSQLServer)
                {
                    MSSQLDataAccess mSSQLDataAccess = new MSSQLDataAccess();
                    result = mSSQLDataAccess.GetTableInformation(CI, TableName);
                }
                else if (CI.DataBaseType == DataBaseType.MSAccess)
                {
                    MSAccessDataAccess mSAccessDataAccess = new MSAccessDataAccess();
                    result = mSAccessDataAccess.GetTableInformation(CI, TableName);
                }
                else if (CI.DataBaseType == DataBaseType.MySQL)
                {
                    MySQLDataAccess mySQLDataAccess = new MySQLDataAccess();
                    result = mySQLDataAccess.GetTableInformation(CI, TableName);
                }

                OnGetTableInformationCompleted(new GetTableInformationEventArgs(result));
            }

            catch (Exception ex)
            {
                OnGetTableInformationCompleted(new GetTableInformationEventArgs(ex));
            }
        }

        public void GetFullDataOfTable(ConnectionInformation CI, ref TableInformation tableInformation)
        {
            try
            {
                if (CI.DataBaseType == DataBaseType.MSSQLServer)
                {
                    MSSQLDataAccess mSSQLDataAccess = new MSSQLDataAccess();
                    mSSQLDataAccess.GetFullData(CI, ref tableInformation);
                }
                else if (CI.DataBaseType == DataBaseType.MSAccess)
                {
                    MSAccessDataAccess mSAccessDataAccess = new MSAccessDataAccess();
                    mSAccessDataAccess.GetFullData(CI, ref tableInformation);
                }
                else if (CI.DataBaseType == DataBaseType.MySQL)
                {
                    MySQLDataAccess mySQLDataAccess = new MySQLDataAccess();
                    mySQLDataAccess.GetFullData(CI, ref tableInformation);
                }

                OnGetFullDataOfTableCompleted(new GetTableInformationEventArgs() { IsFullDataGet = true });
            }
            catch (Exception ex)
            {
                OnGetFullDataOfTableCompleted(new GetTableInformationEventArgs(ex) { IsFullDataGet = false });
            }
        }
    }
}
